//
//  CityTableViewController.m
//  WWTQ
//
//  Created by JP on 2018/10/27.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "CityTableViewController.h"
#import "SelectCityViewController.h"

#import "CityTableViewCell.h"
#import "AddCityTableViewCell.h"

@interface CityTableViewController ()

@property (nonatomic, strong) UIButton *rightBarButtonItem;

@end

static NSString *cityTableViewCellID = @"cityTableViewCellID";
static NSString *addCityTableVIewCellID = @"addCityTableVIewCellID";

@implementation CityTableViewController

- (UIButton *)rightBarButtonItem {
    if (!_rightBarButtonItem) {
        _rightBarButtonItem = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
        [_rightBarButtonItem setTitle:@"编辑" forState:UIControlStateNormal];
        [_rightBarButtonItem setTitle:@"取消" forState:UIControlStateSelected];
        [_rightBarButtonItem setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_rightBarButtonItem setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
        [_rightBarButtonItem addTarget:self action:@selector(_rightBarButtonItemDidClicked:) forControlEvents:UIControlEventTouchUpInside];
        _rightBarButtonItem.titleLabel.font = ZYT16Font;
        _rightBarButtonItem.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    }
    return _rightBarButtonItem;
}

// 编辑按钮被点击
- (void)_rightBarButtonItemDidClicked:(UIButton *)sender {
    sender.selected = !sender.isSelected;
    if (sender.isSelected) {
        // 这个是fix掉:当你左滑删除的时候，再点击右上角编辑按钮， cell上的删除按钮不会消失掉的bug。且必须放在 设置tableView.editing = YES;的前面。
        [self.tableView reloadData];
        // 取消
        [self.tableView setEditing:YES animated:NO];
    } else {
        // 编辑
        [self.tableView setEditing:NO animated:NO];
    }
    [self.tableView reloadData];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initBase];
    
    [self setUpTableView];
    
    [self addRightBarButton];
}

- (void)initBase {
    self.navigationItem.title = @"城市管理";
}

- (void)setUpTableView {
    UIImageView *backImageV = [[UIImageView alloc] initWithImage:self.image];
    self.tableView.backgroundView = backImageV;
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    self.tableView.rowHeight = 50;
    self.tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    self.tableView.allowsMultipleSelection = NO;
    self.tableView.allowsSelectionDuringEditing = NO;
    self.tableView.allowsMultipleSelectionDuringEditing = NO;
    
    //添加高斯效果
    UIToolbar *bar = [[UIToolbar alloc] init];
    bar.barStyle = UIBarStyleDefault;
    bar.alpha = 0.8;
    bar.frame = [UIScreen mainScreen].bounds;
    [backImageV addSubview:bar];
}

- (void)addRightBarButton {
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.rightBarButtonItem];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
//    if (self.tableView.isEditing) {
//        return self.allCityArr.count;
//    }
    return self.allCityArr.count + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.allCityArr.count < 1 || indexPath.row == self.allCityArr.count) {
        AddCityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:addCityTableVIewCellID];
        if (!cell) {
            cell = [[AddCityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addCityTableVIewCellID];
        }
        cell.isEditer = self.tableView.editing;
        return cell;
    }
    CityTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cityTableViewCellID];
    if (!cell) {
        cell = [[CityTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cityTableViewCellID];
    }
    cell.dict = self.allCityArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.allCityArr.count < 1 || indexPath.row == self.allCityArr.count) {
        SelectCityViewController *selectCityVC = [[SelectCityViewController alloc] init];
        selectCityVC.allCityArr = self.allCityArr;
        selectCityVC.image = self.image;
        [self.navigationController pushViewController:selectCityVC animated:YES];
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
     if (editingStyle == UITableViewCellEditingStyleDelete) {
         NSMutableArray *cityMutArr = [NSMutableArray arrayWithArray:self.allCityArr];
         [cityMutArr removeObjectAtIndex:indexPath.row];
         self.allCityArr = [NSArray arrayWithArray:cityMutArr];
         
         // 删除选中项
         [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
         
         [[NSUserDefaults standardUserDefaults] setObject:self.allCityArr forKey:@"AllCityArr"];
         [[NSUserDefaults standardUserDefaults] synchronize];
         
         NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:self.allCityArr, @"allCityArr", nil];
         [[NSNotificationCenter defaultCenter] postNotificationName:DELETECITY object:nil userInfo:cityDic];
     }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    // 删除
    return UITableViewCellEditingStyleDelete;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.allCityArr.count < 1 || indexPath.row == self.allCityArr.count) {
        return NO;
    }
    return YES;
}

//编辑状态下，只要实现这个方法，就能实现拖动排序
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath {
    NSMutableArray *cityMutArr = [NSMutableArray arrayWithArray:self.allCityArr];
    // 取出要拖动的模型数据
    NSDictionary *dict = cityMutArr[sourceIndexPath.row];
    //删除之前行的数据
    [cityMutArr removeObject:dict];
    // 插入数据到新的位置
    [cityMutArr insertObject:dict atIndex:destinationIndexPath.row];
    self.allCityArr = [NSArray arrayWithArray:cityMutArr];
    
    [[NSUserDefaults standardUserDefaults] setObject:self.allCityArr forKey:@"AllCityArr"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    NSDictionary *cityDic = [NSDictionary dictionaryWithObjectsAndKeys:self.allCityArr, @"allCityArr", nil];
    [[NSNotificationCenter defaultCenter] postNotificationName:DELETECITY object:nil userInfo:cityDic];
}

#pragma mark - 修改状态栏颜色
- (UIStatusBarStyle)preferredStatusBarStyle {
    return UIStatusBarStyleDefault;
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}


@end
